package com.chinaunicom.emergency.filter;

import com.chinaunicom.emergency.util.HttpUtil;
import com.chinaunicom.emergency.util.RandomOnly18Util;
import lombok.extern.log4j.Log4j2;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户校验
 * @author liliang
 * @date 2019-12-15
 */
@Order(0)
@Log4j2
//@Configuration()
@WebFilter(filterName = "AuthFilter", urlPatterns = "/*")
public class AuthFilter implements Filter {

    private String url = "";

    @Override
    public void doFilter(ServletRequest request, ServletResponse sresponse, FilterChain filterChain) throws IOException, ServletException {
        ThreadContext.put("requestId", RandomOnly18Util.getInstance().nextId());
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse response = (HttpServletResponse) sresponse;
        ResponseWrapper responseWrapper = new ResponseWrapper(response);
        boolean isFile = false;
        RequestBodyWarpper requestWrapper = null;
        url = httpServletRequest.getRequestURL().toString();
        if (httpServletRequest.getContentType() != null) {
            if (httpServletRequest.getContentType().indexOf("multipart") == -1) {
                requestWrapper = new RequestBodyWarpper(httpServletRequest);
                HttpUtil.printRequest(httpServletRequest, requestWrapper.getBody());
            } else {
                isFile = true;
            }
        } else {
            requestWrapper = new RequestBodyWarpper(httpServletRequest);
            HttpUtil.printRequest(httpServletRequest, requestWrapper.getBody());
        }
//        if(response.getHeader("Access-Control-Allow-Origin") == null){
//            response.setHeader("Access-Control-Allow-Origin", "*");
//            response.setHeader("Access-Control-Expose-Headers", "authorization,token");
//            // 允许请求的方法
//            response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,PUT");
//            // 多少秒内，不需要再发送预检验请求，可以缓存该结果
//            response.setHeader("Access-Control-Max-Age", "3600");
//            // 表明它允许跨域请求包含xxx头
//            response.setHeader("Access-Control-Allow-Headers", "x-auth-token,Origin,Access-Token,X-Requested-With,X_Requested_With,Content-Type,token,Accept,authorization");
//            //是否允许浏览器携带用户身份信息（cookie）
//            response.setHeader("Access-Control-Allow-Credentials", "true");
//        }
//        if ("OPTIONS".equals(httpServletRequest.getMethod())) {
//            response.setStatus(200);
//            return;
//        }

        if (isFile) {
            doFilter(filterChain, httpServletRequest, responseWrapper);
        } else {
            doFilter(filterChain, requestWrapper, responseWrapper);
        }
    }

    public void doFilter(FilterChain filterChain, ServletRequest var1, ResponseWrapper var2) throws IOException, ServletException {
        filterChain.doFilter(var1, var2);
        HttpUtil.printResponse((HttpServletRequest) var1,var2, url);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }

    @Override
    public void destroy() {
    }
}
